package member.login;
import java.io.*;
import java.sql.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import db.member.*;
public class Login extends HttpServlet{
	public void doPost(HttpServletRequest request,
			  HttpServletResponse response){
			  try {
				  requestPro(request,response);
			   } catch (Exception e) {
			    e.printStackTrace();
			   } 
			 }
	 public void doGet(HttpServletRequest request,
	  HttpServletResponse response){
	  try {requestPro(request,response);
	   } catch (Exception e) {
	    e.printStackTrace();
	   }
	 }
	 
	 public void requestPro(HttpServletRequest request,
			  HttpServletResponse response) throws Exception{
		 System.out.println("member.login:Login");
			  MemberDAO dao=new MemberDAO();
			  MemberBean bean=new MemberBean();
			  HttpSession session=request.getSession();
			  int result=-1;
			  request.setCharacterEncoding("euc-kr");
			   bean.setId(request.getParameter("id"));
		   	   bean.setPwd(request.getParameter("pwd"));
		   	   bean.setManager(request.getParameter("manager"));
			   result=dao.empLogin(bean);
			   dao.empName(bean);
			 
			   if(result==0){
		   			response.setContentType("text/html;charset=euc-kr");
			   		PrintWriter out=response.getWriter();
			   		out.println("<script>");
			   		out.println("alert('비밀번호 틀림');");
			   		out.println("location.href='login.jsp';");
			   		out.println("</script>");
			   		out.close();
			   		return;
		   		}else if(result==-1){
		   			response.setContentType("text/html;charset=euc-kr");
			   		PrintWriter out=response.getWriter();
			   		out.println("<script>");
			   		out.println("alert('아이디 없음');");
			   		out.println("location.href='login.jsp';");
			   		out.println("</script>");
			   		out.close();
			   		return;
			   	}
			  bean = dao.empDetail(bean.getId());
			  session.setAttribute("id", bean.getId());
			  session.setAttribute("name", bean.getName());
			  session.setAttribute("pwd", bean.getPwd());
			  session.setAttribute("manager",bean.getManager());
			 
		
			  RequestDispatcher rd= request.getRequestDispatcher("index.jsp");
			  rd.forward(request, response);
			  
			  
			/*  List memberlist=new ArrayList();
				
			  	int page=1;
				int limit=10;
				
				if(request.getParameter("page")!=null){
					page=Integer.parseInt(request.getParameter("page"));
				}
			  int listcount=dao.getmemberCount(); 
			  memberlist=dao.getMemberList(page, limit);
			  
			  int maxpage=(int)((double)listcount/limit+0.95); 
		   		System.out.println("maxpage======"+maxpage);
		   		
		   		int startpage = (((int) ((double)page / 10 + 0.9)) - 1) * 10 + 1;
		   		
		   		int endpage = maxpage;
		   		
		   		if (endpage>startpage+10-1) endpage=startpage+10-1;
		   		
		   	  request.setAttribute("page", page);		  
		   	  request.setAttribute("maxpage", maxpage);
		   	  request.setAttribute("startpage", startpage);
		   	  request.setAttribute("endpage", endpage);    
		   	  request.setAttribute("memberlist", memberlist);
			  
			  RequestDispatcher rd= request.getRequestDispatcher("index.jsp");
					  rd.forward(request, response);*/
		}
}
